using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using TimeWar.StarTrek;
using TimeWar.Daleks;

namespace TimeWar
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary> 
    public class PlayDemoScreen : GameScreen
    {
        protected Dalek dalek = null;
        protected Game gameObj = null;
        protected Starship enterprise_E = null;
        protected Starship XWingFighter = null;
        protected Starship MilleniumFalcon = null;
        protected Starship Tardis = null;
        protected SpriteBatch batch = null;
        protected DemoObjectFactory factory = null;

        public PlayDemoScreen(Game game, GraphicsDeviceManager dm)
            : base(game, dm)
        {

            // TODO: Construct any child components here
            //CM.setRootDirectory("Content");
            gameObj = game;

            if (gameObj != null)
            {
                IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)gameObj.Services.GetService(typeof(IGraphicsDeviceService));
                batch = new SpriteBatch(graphicsService.GraphicsDevice);
            }

            factory = new DemoObjectFactory(gameObj, this);

            dalek = factory.constructDalek();
            enterprise_E = factory.constructFederationStarship();
            //XWingFighter = new Starship(game, "Content\\Models\\XWing");
            //MilleniumFalcon = new Starship(game, "Content\\Models\\MilleniumFalcon");
            //dalek.ModelLocation = "Content\\Models\\DrWho\\Dalek\\Characters\\Dalek";

            dalek.Position = new Vector3(10f, 0f, 100f);
            dalek.Scale = 0.05f;
            //dalek.Chased = false;
            //dalek.Player = PlayerIndex.Two;
            dalek.Mass = 100;

            enterprise_E.Position = new Vector3(10f, 0, 0);
            

            //enterprise_E.CameraPosition = gameCamera.CameraPosition;
            enterprise_E.Scale = 0.025f;

            //enterprise_E.Velocity = new Vector3(1.0f, 1.0f, 1.0f);

            enterprise_E.Update(game.TargetElapsedTime, game.TargetElapsedTime);
            //enterprise_E.Chased = true;
            enterprise_E.Player = PlayerIndex.One;
            //enterprise_E.ObjectType = ObjectTypes.Playable;
            chasedObject = enterprise_E;

            addScreenObject(enterprise_E);
            addScreenObject(dalek);

            screenState = GameState.PlayDemo;

            setupCameras();

            cameraType = CameraTypes.ChaseView;

        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        public override GameState Update(TimeSpan time, TimeSpan elapsedTime)
        {
            XInputHelper.GamePads[PlayerIndex.One].Update(game, Keyboard.GetState());

            if (XInputHelper.GamePads[PlayerIndex.One].State.IsButtonDown(Buttons.Back))
            {
                game.Exit();
            }

            GameState currState = base.Update(time, elapsedTime);

            return currState;

        }

        public override void Render()
        {

            //chaseCamera.Update();

            //SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);
            base.Render();
        }

        public override void onWeaponCollision(CRenderedObject objA, CRenderedObject objB)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        protected override void callObjectUpdateByType(CRenderedObject obj, TimeSpan time, TimeSpan elapsedTime)
        {
            if (obj.ShapeType == ObjectTypes.Starship)
            {
                ((Starship)obj).Update(time, elapsedTime);
            }
            else if (obj.ShapeType == ObjectTypes.Dalek)
            {
                ((Dalek)obj).Update(time, elapsedTime);
            }
            else
            {
                obj.Update(TimeSpan.Zero, TimeSpan.Zero);
            }
                

        }

    }
}